package com.leo.train.member.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.codec.Base64;
import com.leo.train.common.exception.BusinessException;
import com.leo.train.common.exception.BusinessExceptionEnum;
import com.leo.train.common.resp.CommonResp;
import com.leo.train.member.dto.MemberLoginDto;
import com.leo.train.member.dto.MemberPhotoDto;
import com.leo.train.member.dto.MemberSendCodeDto;
import com.leo.train.member.service.MemberService;
import com.leo.train.member.vo.MemberLoginVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/member")
@RefreshScope // 刷新nacos配置
@Slf4j
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${test.nacos}")
    private String testNacos;

    @Autowired
    Environment environment;

    @GetMapping("/")
    public String hello() {
        return "hello world!!!";
    }

    @GetMapping("/hello")
    public String helloNacos() {
        String port = environment.getProperty("local.server.port");
        return String.format("Hello %s! 端口：%s", testNacos, port);
    }
    @GetMapping("/count")
    public CommonResp<Integer> count() {
        int count = memberService.count();
        return new CommonResp(count);
    }

    @PostMapping("/sendCode")
    public CommonResp<Map<String, String>> sendCode(@RequestBody @Valid MemberSendCodeDto req) {
        String code = memberService.sendCode(req);
        log.info("生成的验证码：{}", code);
        // 设置验证有有效期的验证码(第三个参数的时间取决于第四个参数的时间单位) 30秒
        redisTemplate.opsForValue().set("memberCount: " + req.getMobile(), code, 30, TimeUnit.SECONDS);
        Map map = Map.of("code", code);
        return new CommonResp<>(map);
    }

    @GetMapping("/getPhotoCode")
    public CommonResp<Map<String, String>> getCode(@RequestParam Map<String, String> obj) {
        log.info("获取图片验证码：{}", obj);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        String code = lineCaptcha.getCode();
        redisTemplate.opsForValue().set("memberPhotoCode: " + obj.get("mobile"), code);
        Map<String, String> map = new HashMap<>();
        map.put("img", Base64.encode(lineCaptcha.getImageBytes()));
        return new CommonResp<>(map);
    }

    @PostMapping("/judgePhotoCode")
    public CommonResp<String> judgePhotoCode(@RequestBody @Valid MemberPhotoDto req) {
        log.info("判断图片验证码：{}", req);
        String code = (String) redisTemplate.opsForValue().get("memberPhotoCode: " + req.getMobile());
        if (!code.equals(req.getPhotoCode())) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_PHOTO_CODE_ERROR);
        }
        return new CommonResp<>("");
    }

    @PostMapping("/login")
    public CommonResp<MemberLoginVO> login(@RequestBody @Valid MemberLoginDto req) {
        log.info("会员登录：{}", req);
        MemberLoginVO memberLoginVO = memberService.login(req);
        return new CommonResp<>(memberLoginVO);
    }
}
